<template>
    <el-dialog title="新增用户组" :visible.sync="addUserGroupDialogVisible" :before-close="cancel" :close-on-click-modal="false" class="vc-dialog-form" width="480px">
        <el-form ref="addForm" :model="addForm" :rules="rules" label-width="100px" size="small">

            <input type="hidden" v-model="addForm.pid" maxlength="10" readonly></input>

            <el-form-item label="用户组名称" prop="name">
                <el-input v-model="addForm.name" maxlength="20" clearable></el-input>
            </el-form-item>

        </el-form>

        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="cancel">取消</el-button>
            <el-button type="primary" size="small" :disabled="disabled" @click="save">保存</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { validateInput } from '@/utils/validator'
export default {
  name: 'addUserGroup',

  props: ['addUserGroupDialogVisible', 'pid'],
  data () {
    return {
      addForm: {
        pid: '',
        name: ''
      },
      rules: {

        name: [
          {
            required: true,
            message: '请输入用户组名称',
            trigger: 'blur'
          },
          { validator: validateInput, trigger: 'blur' }

        ]
      },
      disabled: false,
      readonly: true
    }
  },
  computed: {
    editMenuData () {
      return this.$parent.editMenuData
    },
    ugTreeData () {
      var temp = this.$parent.ugTreeDataHasTop

      return temp
    }
  },
  watch: {
    addUserGroupDialogVisible (val) {
      if (!val) return
      this.$nextTick(() => {
        this.$refs.addForm && this.$refs.addForm.resetFields()

        this.addForm.pid = this.pid
        setTimeout(() => {
          this.$refs.addForm.clearValidate()
        }, 0)
      })
    }
  },
  methods: {
    /**
         * 保存
         */
    save () {
      if (this.disabled) {
        return
      }
      this.$refs.addForm.validate(valid => {
        if (!valid) {
          return false
        }
        this.disabled = true
        this.$api
          .addUgroupByPid({
            data: this.addForm
          })
          .then(res => {
            this.disabled = false
            if (res.success) {
              this.$emit('close', res.data || {})
              this.$notifyBox('新增成功', 'success')
            }
          })
          .catch(err => {
            this.disabled = false
          })
      })
    },
    /**
         * 取消
         */
    cancel () {
      this.$emit('close', false)
    }
  }
}
</script>
